package com.lagou.task.dao;
import com.lagou.task.entity.SysClass;
import com.lagou.task.utils.Dbutils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class ClassDaoImpl implements ClassDao{
    /**
     * 新增班级实现方法
     * @param sysClass
     * @return
     */
    @Override
    public int addClass(SysClass sysClass) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = Dbutils.getConnection();
            String sql = "insert into class values (?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,sysClass.getName());
            preparedStatement.setString(2,sysClass.getGrade());
            preparedStatement.setString(3,sysClass.getTeacher());
            preparedStatement.setString(4,sysClass.getSlogan());
            preparedStatement.setInt(5,sysClass.getNumer());
            int update = preparedStatement.executeUpdate();
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Dbutils.close(connection,preparedStatement,resultSet);
        }
        return 0;
    }

    /**
     * 删除班级实现方法
     * @param sysClass
     * @return
     */
    @Override
    public int deleteClass(SysClass sysClass) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = Dbutils.getConnection();
            String sql = "delete from class where name = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,sysClass.getName());
            int update = preparedStatement.executeUpdate();
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Dbutils.close(connection,preparedStatement,resultSet);
        }
        return 0;
    }

    /**
     * 修改班级信息方法
     * @param sysClass
     * @return
     */
    @Override
    public int modifyClass(SysClass sysClass) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = Dbutils.getConnection();
            String sql = "update class set grade=? , teacher=? , slogan=? , number=? where name=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,sysClass.getGrade());
            preparedStatement.setString(2,sysClass.getTeacher());
            preparedStatement.setString(3,sysClass.getSlogan());
            preparedStatement.setInt(4,sysClass.getNumer());
            preparedStatement.setString(5,sysClass.getName());
            int update = preparedStatement.executeUpdate();
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Dbutils.close(connection,preparedStatement,resultSet);
        }
        return 0;
    }

    /**
     * 根据班级名查找班级
     * @param className
     * @return
     */
    @Override
    public SysClass findClass(String className) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = Dbutils.getConnection();
            String sql = "select * from class where name = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,className);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                SysClass sysClass = new SysClass(resultSet.getString("name"),
                        resultSet.getString("grade"),
                        resultSet.getString("teacher"),
                        resultSet.getString("slogan"),
                        resultSet.getInt("number"));
                return sysClass;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Dbutils.close(connection,preparedStatement,resultSet);
        }
        return null;
    }

    /**
     * 返回所有班级
     * @return
     */
    @Override
    public List<SysClass> showAll() {
        List<SysClass> classes = new ArrayList<>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = Dbutils.getConnection();
            String sql = "select * from class";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            if (!resultSet.wasNull()){
                while (resultSet.next()){
                    SysClass sysClass = new SysClass(resultSet.getString("name"),
                            resultSet.getString("grade"),
                            resultSet.getString("teacher"),
                            resultSet.getString("slogan"),
                            resultSet.getInt("number"));
                    classes.add(sysClass);
                }
                classes.sort(Comparator.comparingInt(x -> Integer.parseInt(x.getName())));
                return classes;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Dbutils.close(connection,preparedStatement,resultSet);
        }
        return null;
    }

    /**
     * 计算班级人数实现方法
     * @param sysClass
     * @return
     */
    @Override
    public int culculateClassNumber(SysClass sysClass) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = Dbutils.getConnection();
            String sql = "select * from student where className = ? ";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,sysClass.getName());
            resultSet = preparedStatement.executeQuery();
            int number = 0;
            if (!resultSet.wasNull()){
                while (resultSet.next()){
                    number += 1;
                }
            }
            return updateClassNumber(sysClass,number);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Dbutils.close(connection,preparedStatement,resultSet);
        }
        return 0;
    }

    public int updateClassNumber(SysClass sysClass,int number){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = Dbutils.getConnection();
            String sql = "update class set number=? where name=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,number);
            preparedStatement.setString(2,sysClass.getName());
            int update = preparedStatement.executeUpdate();
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Dbutils.close(connection,preparedStatement,resultSet);
        }
        return 0;
    }
}
